package cn.com.gridinfo.sooc.core.expand.callname.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jeremy.arad.utils.AnimUtil;
import com.jeremy.arad.utils.Log;
import com.marshalchen.ultimaterecyclerview.UltimateRecyclerviewViewHolder;
import com.marshalchen.ultimaterecyclerview.UltimateViewAdapter;

import java.util.List;

import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.core.expand.callname.activity.SignInfoActivity;
import cn.com.gridinfo.sooc.core.expand.callname.activity.TongjiInfoActivity;
import cn.com.gridinfo.sooc.core.expand.callname.bean.ClassInfo;
import cn.com.gridinfo.sooc.utils.DateUtil;


/**
 * 历史点名的adapter
 * Created by yingchunli
 */
public class HistoryCallnameAdapter extends UltimateViewAdapter<HistoryCallnameAdapter.ViewHolder> {

    public static List<ClassInfo> classInfoList;
    private Context context;

    public HistoryCallnameAdapter(List<ClassInfo> classInfoList, Context context) {
        this.classInfoList = classInfoList;
        this.context = context;
    }

    //加载更多
    public void loadmore(List<ClassInfo> list) {
        classInfoList.addAll(list);
        notifyDataSetChanged();
    }

    //刷新
    public void refresh(List<ClassInfo> list) {
        classInfoList.clear();
        classInfoList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder getViewHolder(View view) {
        return new ViewHolder(view, false);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent) {
        //加载布局
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.expand_callname_historycallname_item, parent, false);
        ViewHolder holder = new ViewHolder(view, true);
        view.setOnClickListener(holder);
        return holder;
    }

    @Override
    public int getAdapterItemCount() {
        return classInfoList.size();
    }

    @Override
    public long generateHeaderId(int position) {
        return position;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (position < getItemCount() &&
                (customHeaderView != null ? position <= classInfoList.size() : position < classInfoList.size())
                && (customHeaderView != null ? position > 0 : true)) {
            ClassInfo classInfo = classInfoList.get(position);
            holder.kcmc.setText(classInfo.getKcmc());
            holder.ktzt.setText(classInfo.getKtzt());
            if (classInfo.getSkdd().isEmpty() || classInfo.getSkdd() == null) {
                //没有上课地点
                holder.skdd.setVisibility(View.GONE);
            } else {
                //有上课地点
                holder.skdd.setVisibility(View.VISIBLE);
                holder.skdd.setText(classInfo.getSkdd());
            }
            holder.sksj.setText(DateUtil.getDateFromDatestamp(classInfo.getSkrq()));
            if (classInfo.getPzqd() == 0) {
                //不用拍照签到
                holder.pzqd.setVisibility(View.GONE);
            } else {
                //拍照签到
                holder.pzqd.setVisibility(View.VISIBLE);
            }
            holder.qxrs.setText(classInfo.getQxrs() + "");
            holder.qdrs.setText(classInfo.getQdrs() + "");
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        return null;
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {
    }

    //清空数据
    public void clear(){
        classInfoList.clear();
        notifyDataSetChanged();
    }

    /**
     * 点击事件接口
     */
    public interface OnItemClickListener {
        void OnItemClick(View view, ClassInfo classInfo,int position);
    }

    private OnItemClickListener mOnItemClickLitener;

    public void setmOnItemClickLitener(OnItemClickListener mOnItemClickLitener) {

        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    public class ViewHolder extends UltimateRecyclerviewViewHolder implements View.OnClickListener {
        TextView kcmc, skdd, ktzt, sksj, pzqd, qxrs, qdrs;

        public ViewHolder(View itemView, boolean isItem) {
            super(itemView);
            if (isItem) {
                kcmc = (TextView) itemView.findViewById(R.id.kcmc_historycallname_item);//课程名称
                skdd = (TextView) itemView.findViewById(R.id.skdd_historycallname_item);//上课地点
                sksj = (TextView) itemView.findViewById(R.id.time_historycallname_item);//上课时间
                ktzt = (TextView) itemView.findViewById(R.id.ktzt_histotycallname_item);//课程主题
                pzqd = (TextView) itemView.findViewById(R.id.pzqd_historycallname_item);//拍照签到
                qdrs = (TextView) itemView.findViewById(R.id.sdrs_historycallname_item);//签到人数
                qxrs = (TextView) itemView.findViewById(R.id.qxrs_historycallname_item);//缺席人数
            }
        }


        @Override
        public void onClick(View v) {
            mOnItemClickLitener.OnItemClick(v,classInfoList.get(getAdapterPosition()),getAdapterPosition());
        }
    }

}
